package org.example.state;

import com.google.common.collect.Lists;
import org.apache.flink.api.common.eventtime.TimestampAssignerSupplier;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.api.common.state.MapState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;
import org.example.flink.pojo.WaterSensor;
import org.example.function.WaterSensorFunction;

import java.time.Duration;
import java.util.ArrayList;

/**
 * @Author: tang
 * @Description:
 * @Date 2025/2/24 16:51
 */
public class MapValueStateDemo {

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment environment = StreamExecutionEnvironment.getExecutionEnvironment();

        environment.setParallelism(1);

        SingleOutputStreamOperator<WaterSensor> streamOperator = environment
                .socketTextStream("127.0.0.1", 7777)
                .map(new WaterSensorFunction())
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy.<WaterSensor>forBoundedOutOfOrderness(Duration.ofSeconds(2))
                                .withTimestampAssigner((TimestampAssignerSupplier<WaterSensor>)
                                context -> (element, recordTimestamp) -> element.getVc() * 1000L)
                );

        streamOperator
                .keyBy(WaterSensor::getId)
                .process(
                        new KeyedProcessFunction<String, WaterSensor, String>() {

                            // 定义状态
                            private MapState<String,Integer> mapState;

                            @Override
                            public void open(Configuration parameters) throws Exception {
                                super.open(parameters);
                                // 初始化 mapState，只能在这个地方初始化
                                mapState = getRuntimeContext()
                                        .getMapState(new MapStateDescriptor<>("Integer",Types.STRING, Types.INT));
                            }

                            @Override
                            public void processElement(WaterSensor value,
                                                       KeyedProcessFunction<String, WaterSensor, String>.Context ctx,
                                                       Collector<String> out) throws Exception {
                                if (!mapState.contains(value.getVt())) {
                                    mapState.put(value.getVt(),1);
                                }else {
                                    mapState.put(value.getVt(), mapState.get(value.getVt()) + 1);
                                }
                            }
                        }
                ).print();

        environment.execute();

    }

}
